<?php

namespace App\Http\Controllers\Api;

use App\Model\FirstUserRecharge;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class FirstUserRechargeController extends BaseController
{
    protected $first_user_recharge_model;//获取用户首充值记录

    public function __construct()
    {
        $this->first_user_recharge_model = new FirstUserRecharge();
    }
    /**
     * 获取当日首充用户数据情况
     */
    public function getDailyRechargeList(Request $request)
    {
        //数据整理
        $data = $this->first_user_recharge_model->getDailyRechargeList();
        //查询当日领取的券数
        foreach($data as $k=>$v)
        {
            $ticketCount = $this->first_user_recharge_model->getDailyTicketNums($v['create_time']);
            $data[$k]['ticket_nums'] = $ticketCount;
            //获取使用次数
            $useCount = $this->first_user_recharge_model->getDailyTicketUseCount($v['create_time']);
            $data[$k]['ticket_use_count'] = $useCount;
            //消耗钻石数
            $usediamonCount = $this->first_user_recharge_model->getUsedDiamonValue($v['create_time']);
            $data[$k]['use_diamon_count'] = $usediamonCount;
            //兑换金豆
            $goldValue = $this->first_user_recharge_model->getUsedGoldValue($v['create_time']);
            $data[$k]['gold_count'] = $goldValue;
        }
        return $this->toJson(self::SUCCESS, $data);
    }

    /**
     * 次日首充用户统计
     */
    public function getTomorrowRechargeList(Request $request)
    {
        //数据整理
        $data = $this->first_user_recharge_model->getDailyRechargeList();
        //查询次日用户留存率
        foreach($data as $k=>$v)
        {
            //次日首充用户数
            $tomorrowUserCount = $this->first_user_recharge_model->getTomorrowUserCount($v['create_time']);
            $data[$k]['tomorrow_recharge_rate'] = 0;
            $data[$k]['tomorrow_recharge_money'] = 0;
            $data[$k]['beforeThreeUsers_rate'] = 0;
            $data[$k]['beforeSevenUsers_rate'] = 0;
            $data[$k]['usernums'] = 0;
            if(!empty($tomorrowUserCount))
            {
                $data[$k]['usernums'] = $tomorrowUserCount; //第二天统计前一天留存充值用户
              //前一日留存的使用许愿卡用户
              $yestordayUserRecharge = $this->first_user_recharge_model->getUserRecharge($v['create_time'],1);
              if(!empty($yestordayUserRecharge['aftercount']))
              {
                $data[$k]['tomorrow_recharge_rate'] = round(($yestordayUserRecharge['aftercount'] /$yestordayUserRecharge['count'])*100).'%';  
              }
              //前一日充值金额
              $data[$k]['tomorrow_recharge_money'] = $yestordayUserRecharge['money']/100;
            
            }
            //前3日留存人数
            $threeDays = $this->first_user_recharge_model->getUserRecharge($v['create_time'],3);
            
            $data[$k]['beforeThreeUsers'] = $threeDays['savecount'];
            //前3日充值留存率
            if(!empty($threeDays['aftercount']))
            {
                $data[$k]['beforeThreeUsers_rate'] = round(($threeDays['aftercount']/$threeDays['count'])*100).'%'; 
            }
            //3日充值数
            $data[$k]['beforeThreeUsers_money'] = $threeDays['money']/100;
            //7日留存人数
            $sevenDays = $this->first_user_recharge_model->getUserRecharge($v['create_time'],7);
            $data[$k]['beforeSevenUsers'] = $sevenDays['savecount'];
            //前7日充值留存率
            if(!empty($sevenDays['aftercount']))
            {
                $data[$k]['beforeSevenUsers_rate'] = round(($sevenDays['aftercount']/$sevenDays['count'])*100).'%'; 
            }
            //7日充值数
            $data[$k]['beforeSevenUsers_money'] = $sevenDays['money']/100;
        }
        return $this->toJson(self::SUCCESS, $data);
    }
    
    /**
     * 获取用户首充送礼统计
     */
    public function getRechargeSendList(){
        //获取统计列表
        $data = $this->first_user_recharge_model->getFirstRechargeSendLog();
        return $this->toJson(self::SUCCESS, $data);
    }
}


